package cn.com.cetccst.appstore.ui.activity;

import static cn.com.cetccst.tools.device.Density.dip2px;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import cn.com.cetccst.appstore.BR;
import cn.com.cetccst.appstore.R;
import cn.com.cetccst.appstore.databinding.ActivityPreviewBinding;
import cn.com.cetccst.appstore.databinding.AdapterPreviewItemBinding;
import cn.com.cetccst.appstore.ui.activity.viewmodel.PreviewViewModel;
import cn.com.cetccst.uicomponents.binding.app.DataBindingConfig;
import cn.com.cetccst.uicomponents.binding.recyclerview.SimpleDataBindingAdapter;

public class PreviewActivity extends BaseActivity<ActivityPreviewBinding> {

    private PreviewViewModel viewModel;

    public final static String EXTRA_IMAGE_URL_LIST = "extra_image_url_list";
    public final static String EXTRA_CURRENT_IMAGE = "extra_current_image";
    private List<ImageView> dots;

    @Override
    protected void initViewModel() {
        viewModel = getActivityScopeViewModel(PreviewViewModel.class);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.activity_preview, BR.viewModel, viewModel)
                .addBindingParam(BR.adapter, new ImageListAdapter())
                .addBindingParam(BR.pageChangeListener, new ViewPager2.OnPageChangeCallback() {
                    @Override
                    public void onPageSelected(int position) {
                        for (ImageView imageView : dots) {
                            imageView.setImageResource(R.drawable.shape_preview_dot_unselected);
                        }
                        //设置当前显示的页面的dot设置为dot_focused
                        dots.get(position).setImageResource(R.drawable.shape_preview_dot_selected);
                    }
                })
                .addBindingParam(BR.context, this);
    }

    @Override
    protected void onResume() {
        super.onResume();

        hideBottomUIMenu();//隐藏虚拟按键

        Objects.requireNonNull(getSupportActionBar()).setDisplayShowTitleEnabled(false);

        viewModel.currentPosition.setValue(getIntent().getIntExtra(EXTRA_CURRENT_IMAGE, 0));
        viewModel.list.setValue(getIntent().getStringArrayListExtra(EXTRA_IMAGE_URL_LIST));

        initData();
    }

    private void initData() {

        //通过循环动态的添加点。
        dots = new ArrayList<>();
        // only more than 1 has dot display
        if (viewModel.list.getValue().size() > 1) {
            for (int i = 0; i < viewModel.list.getValue().size(); i++) {
                ImageView imageView = new ImageView(this);
                int width = dip2px(this, 10);
                int height = dip2px(this, 10);
                int margin = dip2px(this, 10);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
                params.setMargins(margin, margin, margin, margin);//设置margin,也就是外边距。
                imageView.setLayoutParams(params);//传入参数params设置宽和高
                imageView.setImageResource(R.drawable.shape_preview_dot_unselected);//设置图片
                getBinding().linearlayoutDot.addView(imageView);//将图片添加到布局中
                //将dot添加到dots集合中
                dots.add(imageView);
            }

            dots.get(viewModel.currentPosition.getValue()).setImageResource(R.drawable.shape_preview_dot_selected);//设置启动后显示的第一个点
        }
    }


    /**
     * 隐藏虚拟按键，并且全屏
     */
    protected void hideBottomUIMenu() {
        //隐藏虚拟按键，并且全屏
        //for new api versions.
        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        decorView.setSystemUiVisibility(uiOptions);
    }

    class ImageListAdapter extends SimpleDataBindingAdapter<String, AdapterPreviewItemBinding> {

        public ImageListAdapter() {
            super(PreviewActivity.this, R.layout.adapter_preview_item, new DiffUtil.ItemCallback<>() {
                @Override
                public boolean areItemsTheSame(@NonNull String oldItem, @NonNull String newItem) {
                    return Objects.equals(oldItem, newItem);
                }

                @Override
                public boolean areContentsTheSame(@NonNull String oldItem, @NonNull String newItem) {
                    return Objects.equals(oldItem, newItem);
                }
            });
        }

        @Override
        protected void onBindItem(AdapterPreviewItemBinding binding, String item, RecyclerView.ViewHolder holder) {
            Glide.with(PreviewActivity.this)
                    .load(item)
                    .into(binding.screenshotImage);
        }
    }
}
